package com.woniuxy.sddfp.access.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.woniuxy.sddfp.access.entity.TradeLog;
import com.woniuxy.sddfp.access.service.ITradeLogService;
import com.woniuxy.sddfp.common.model.BaseResult;
import com.woniuxy.sddfp.common.model.dto.bank.BankCardAddDTO;
import com.woniuxy.sddfp.common.model.dto.bank.BankCardRequestDto;
import com.woniuxy.sddfp.common.model.dto.bank.SignatureDto;
import com.woniuxy.sddfp.common.model.dto.bid.BankBidDTO;
import com.woniuxy.sddfp.common.model.dto.bid.BidBalanceDTO;
import com.woniuxy.sddfp.common.model.vo.trade.BorrowSyncRecordVo;
import com.woniuxy.sddfp.common.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 托管系统交易记录表 前端控制器
 * </p>
 *
 * @author yellow doctor
 * @since 2025-06-25
 */
@RestController
@RequestMapping("/trade-log")
@Slf4j
public class TradeLogController {

    @Autowired
    private ITradeLogService tradeLogService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 绑卡
     */
    @PostMapping("/bank/access/card")
    public SignatureDto proxyBindCardLog(@RequestBody BankCardAddDTO bankCardAddDTO) {
        SignatureDto signatureDto = tradeLogService.proxyBindCardLog(bankCardAddDTO);
        return signatureDto;
    }

    /**
     * 银行托管系统回调接口
     */
    @PostMapping("/bank/access/receive")
    public String callbackUpdate(@RequestBody BankCardRequestDto bankCardRequestDto) {

        //01 修改日志     trade-log 中的  状态  setSynchronizeStatus ,  ConfirmDate
        tradeLogService.update(
                Wrappers.lambdaUpdate(TradeLog.class)
                      .set(TradeLog::getSynchronizeStatus,1)
                      .set(TradeLog::getConfirmDate,new Date())
                      .eq(TradeLog::getReqCode,bankCardRequestDto.getReqCode())
        );
        //02 发送数据到mq

        rabbitTemplate.convertAndSend(
                "sddfp-exchange-bind",
                "bind-card-message",
                bankCardRequestDto);

        return "mq的数据发送成功...";
    }

    /**
     * 借款记录审核，同步借款数据到银行托管系统
     */
    @ApiOperation("借款记录审核，同步数据")
    @PostMapping("/bank/access/sync/borrow")
    public Result syncBorrowRecord(@RequestBody BorrowSyncRecordVo borrowSyncRecordVo) {
        log.info("trade 平台 发送数据:{}",borrowSyncRecordVo);
        BaseResult baseResult = tradeLogService.syncBorrowRecord(borrowSyncRecordVo);
        return Result.ok(baseResult);
    }

    @ApiOperation("获取机构账户余额信息")
    @GetMapping("/bank/access/bid/balance/{userCode}")
    public Result<BidBalanceDTO> getBidBalance(@PathVariable("userCode") String userCode) {
        BidBalanceDTO balanceDetailsDTO = tradeLogService.getBidBalance(userCode);
        return Result.ok(balanceDetailsDTO);
    }

    @ApiOperation("银行保存投资认购记录")
    @PostMapping("/bank/access/bid/add")
    public Result<String> bankBidService(@RequestBody BankBidDTO bankBidDTO) {
        log.info("托管代理系统，接收到请求参数：{}",bankBidDTO);
        BaseResult baseResult =
                tradeLogService.bankBidService(bankBidDTO);

        Result<String> result=new Result<>();
        result.setCode(200);
        result.setMessage(baseResult.getRespMsg());
        return result;
    }



}
